import MD5JS from '../utils/md5.js';
import axios from 'axios';
import store from '../store';

const getParams = (signParam = "", jsp) => {
  let key = store.getters.sidebar.apiKey
  var signMD5 = MD5JS.hexMD5(signParam + "&key=" + key)
  let Param = signParam + "&sign=" + signMD5
  let dir = store.getters.sidebar.dirPart
  // let jsp = store.getters.sidebar.mallJsp
  let url = store.getters.sidebar.Url + dir + jsp
  // console.log("sin值",signMD5)
  return { url, Param }
}
//首页分类
const gettpye = (p) => {
  console.log("这个方法被调用")
  let appId = store.getters.sidebar.appId
  let timestamp = Date.parse(new Date()) / 1000
  let signParam = 'cls=product_goodtype&action=QueryTypes&appId=' + appId + '&timestamp=' + timestamp
  let jsp = store.getters.sidebar.mallJsp
  let { url, Param } = getParams(signParam, jsp)
  return new Promise((resolve, reject) => {
    axios.get(url + Param, {
      params: p
    })
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data)
      })
  });
}
//获取首页拼团列表
const getptlist = (p) => {
  console.log("这个方法被调用")
  let appId = store.getters.sidebar.appId
  let timestamp = Date.parse(new Date()) / 1000
  let signParam = 'cls=product_goodtype&action=QuerySpell&appId=' + appId + '&timestamp=' + timestamp
  let jsp = store.getters.sidebar.mallJsp
  let { url, Param } = getParams(signParam, jsp)
  return new Promise((resolve, reject) => {
    axios.get(url + Param, {
      params: p
    })
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data)
      })
  });
}
//获取首页tabbat
const getbanner = (p) => {
  let appId = store.getters.sidebar.appId
  let timestamp = Date.parse(new Date()) / 1000
  let signParam = 'cls=main_gbanner&action=gbannerList&appId=' + appId + '&timestamp=' + timestamp
  let jsp = store.getters.sidebar.otherJsp
  let { url, Param } = getParams(signParam, jsp)
  return new Promise((resolve, reject) => {
    axios.get(url + Param, {
      params: p
    })
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data)
      })
  });
}
//获取商品详情
const getgoodinfo = (p) => {
  let appId = store.getters.sidebar.appId
  let timestamp = Date.parse(new Date()) / 1000
  let signParam = 'cls=product_goodtype&action=QuerySpellDetail&appId=' + appId + '&timestamp=' + timestamp
  let jsp = store.getters.sidebar.mallJsp
  let { url, Param } = getParams(signParam, jsp)
  return new Promise((resolve, reject) => {
    axios.get(url + Param, {
      params: p
    })
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data)
      })
  });
}
//获取用户收货地址
const getuserad = (p) => {
  let appId = store.getters.sidebar.appId
  let timestamp = Date.parse(new Date()) / 1000
  let signParam = 'cls=product_address&action=selectAddress&userId=1&appId=1&timestamp=' + timestamp
  let jsp = store.getters.sidebar.mallJsp
  let { url, Param } = getParams(signParam, jsp)
  return new Promise((resolve, reject) => {
    axios.get(url + Param, {
      params: p
    })
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data)
      })
  });
}
//获取滚动弹幕
const rollword = (p) => {
  let appId = store.getters.sidebar.appId
  let timestamp = Date.parse(new Date()) / 1000
  let signParam = 'cls=product_shopapply&action=QuerySpellHot2&appId=' + appId + '&timestamp=' + timestamp
  let jsp = store.getters.sidebar.mallJsp
  let { url, Param } = getParams(signParam, jsp)
  return new Promise((resolve, reject) => {
    axios.get(url + Param, {
      params: p
    })
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data)
      })
  });
}
//区的数据  快拼区  新人区.....
const getlist = (p) => {
  let appId = store.getters.sidebar.appId
  let timestamp = Date.parse(new Date()) / 1000
  let signParam = 'cls=product_goodtype&action=QuerySpell&appId=' + appId + '&timestamp=' + timestamp
  let jsp = store.getters.sidebar.mallJsp
  let { url, Param } = getParams(signParam, jsp)
  return new Promise((resolve, reject) => {
    axios.get(url + Param, {
      params: p
    })
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data)
      })
  });
}
//根据商品id等信息获取商品详情
const getgoodinfo2 = (p) => {
  // console.log("info2被执行",p)
  let appId = store.getters.sidebar.appId
  let timestamp = Date.parse(new Date()) / 1000
  let signParam = 'cls=product_goodtype&action=QuerySpell&appId=' + appId + '&timestamp=' + timestamp
  let jsp = store.getters.sidebar.mallJsp
  let { url, Param } = getParams(signParam, jsp)
  return new Promise((resolve, reject) => {
    axios.get(url + Param, {
      params: p
    })
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data)
      })
  });
}
//新增订单接口
const addorder = (p) => {
  let appId = store.getters.sidebar.appId
  let timestamp = Date.parse(new Date()) / 1000
  let signParam = 'cls=product_order&action=SaveOrders&appId=' + appId + '&timestamp=' + timestamp
  let jsp = store.getters.sidebar.mallJsp
  let { url, Param } = getParams(signParam, jsp)
  return new Promise((resolve, reject) => {
    axios.get(url + Param+'&shareuserId=0', {
      params:p
    })
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data)
      })
  });
}
//获取订单详情
const getorderInfo=(p)=>{
  let appId = store.getters.sidebar.appId
  let timestamp = Date.parse(new Date()) / 1000
  let signParam = 'cls=product_order&action=QueryOrdersDetailNew&appId=' + appId + '&timestamp=' + timestamp
  let jsp = store.getters.sidebar.mallJsp
  let { url, Param } = getParams(signParam, jsp)
  return new Promise((resolve, reject) => {
    axios.get(url + Param, {
      params: p
    })
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data)
      })
  });
}
//获取支付参数
const getpayzt=(p)=>{
  let appId = store.getters.sidebar.appId
  let timestamp = Date.parse(new Date()) / 1000
  let signParam = 'cls=shoppingSource_shoppingSource&action=findShoppingMallSource&sm_companyId=' + p.sm_companyId+ "&wx_appId=" + p.wx_appId +'&appId=' + appId  +'&timestamp=' + timestamp
  let jsp = store.getters.sidebar.mallJsp
  let { url, Param } = getParams(signParam, jsp)
  return new Promise((resolve, reject) => {
    axios.get(url + Param, {
      params: ""
    })
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data)
      })
  });
}
//预支付id获取
const beforpayment=(p)=>{
  let appId = store.getters.sidebar.appId
  let timestamp = Date.parse(new Date()) / 1000
  let signParam = 'cls=main_payment&action=BJYXCXpayment&appId=' + appId + '&timestamp=' + timestamp
  let jsp = store.getters.sidebar.otherJsp
  let { url, Param } = getParams(signParam, jsp)
  return new Promise((resolve, reject) => {
    axios.get(url + Param, {
      params: p
    })
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data)
      })
  });
}
//接口导出
export {
  getAffiche,
  gettpye,
  getbanner,
  getgoodinfo,
  getgoodinfo2,
  getlist,
  getuserad,
  rollword,
  getptlist,
  addorder,
  getorderInfo,
  getpayzt,
  beforpayment
}